import { doUserInsert } from "./insert.1.user.master";
import { doBrandInsert } from "./insert.2.brand";
import { doContactInsert } from "./insert.3.contact";
import { doTonTplInsert } from "./insert.4.tpl";
import { doTonOrderSaleInsert } from "./insert.5.order.sale";
import { doTonSkuSaleInsert } from "./insert.6.sku.sale";
import { doTonSkuGetoutInsert } from "./insert.7.sku.getout";

import { doTonOrderPurInsert } from "./insert.8.order.purchase";
import { doTonSkuPurInsert } from "./insert.9.sku.purchase";
import { doTonSkuGetinInsert } from "./insert.10.sku.getin";

// import { doTonOrderAdjustInsert } from "./insert.11.order.adjust";
// import { doTonSkuAdjustInsert } from "./insert.12.sku.adjust";
import { doVoucherInsert } from "./insert.13.voucher";
import { doVoucherOrderInsert } from "./insert.14.voucher-order";
import { doBookSumInsert } from "./insert.15.book-map";

const superUserId = "";
const mongo_master_id = "";
const mongo_master_corp_id = "";

(async () => {
  try {
    // ⏰1 User / RefUserSub / UserPassword 主账号/子账号/映射关系/，它们的密码
    const { _id_to_user_map } = await doUserInsert(superUserId, mongo_master_id, mongo_master_corp_id);

    // ⏰2 Paster 贴纸
    await doBrandInsert(mongo_master_corp_id, _id_to_user_map);

    // ⏰3 ContactGroup / Contact（客户/供应商/其他/财务往来）
    //（1）新系统的 Contact.content = 旧系统的 Contact._id
    const { _id_to_contact_map } = await doContactInsert(mongo_master_corp_id, _id_to_user_map);

    // ⏰4 Formula / TonTplGroup / TonTpl
    await doTonTplInsert(mongo_master_corp_id, _id_to_user_map);

    // ⏰5 TonOrderGroup / TonOrder / RefTonOrderStamp / Order > Book
    //（1）新系统的 TonOrder.content = 旧系统的 Order._id
    const { _id_to_sale_order_map, master_to_order_group_map, pg_uuid32_sale_order_map } = await doTonOrderSaleInsert(
      mongo_master_corp_id,
      _id_to_user_map,
      _id_to_contact_map,
    );

    // ⏰6 TonSku（Sale）
    const { pg_sale_order_uuiid32_to_skus_map } = await doTonSkuSaleInsert(mongo_master_corp_id, _id_to_user_map, _id_to_sale_order_map);

    // ⏰7 TonSku（GetOut）OrderStock（count/ton）
    await doTonSkuGetoutInsert(mongo_master_corp_id, _id_to_user_map, _id_to_sale_order_map, pg_sale_order_uuiid32_to_skus_map, pg_uuid32_sale_order_map);

    // ⏰8 TonOrderGroup / TonOrder / RefTonOrderStamp / Order > Book
    //（1）新系统的 TonOrder.content = 旧系统的 Order._id
    const { _id_to_buy_order_map, pg_uuid32_buy_order_map } = await doTonOrderPurInsert(
      mongo_master_corp_id,
      _id_to_user_map,
      _id_to_contact_map,
      master_to_order_group_map,
    );

    // ⏰9 TonSku（Purchase）
    const { pg_buy_order_uuiid32_to_skus_map } = await doTonSkuPurInsert(mongo_master_corp_id, _id_to_user_map, _id_to_buy_order_map);

    // ⏰10 TonSku（GetIn）OrderStock（count/ton）
    await doTonSkuGetinInsert(mongo_master_corp_id, _id_to_user_map, _id_to_buy_order_map, pg_buy_order_uuiid32_to_skus_map, pg_uuid32_buy_order_map);

    // // const { oldToNewAdjustMap } = await doTonOrderAdjustInsert(mongo_master_corp_id, _id_to_user_map, _id_to_contact_map, userOrderGroupMap);
    // // await doTonSkuAdjustInsert(mongo_master_corp_id, _id_to_user_map, oldToNewAdjustMap);

    // ⏰13 Voucher / VoucherGroup / Paster / ContactGroup / Contact
    const { _id_to_voucher_map } = await doVoucherInsert(mongo_master_corp_id, _id_to_user_map, _id_to_contact_map);

    // ⏰14 Book
    await doVoucherOrderInsert(mongo_master_id, _id_to_user_map, _id_to_sale_order_map, _id_to_buy_order_map, _id_to_voucher_map);

    // ⏰15 BookSum
    await doBookSumInsert(mongo_master_corp_id, _id_to_user_map);
  } catch (error) {
    console.log(error);
  } finally {
    process.exit();
  }
})();
